package com.rmi;

import com.controller.broker.BrokerCORBAClient;
import com.controller.broker.BrokerRMIClient;
import com.dao.Flight;
import com.dao.User;
import com.dao.UserDaoImpl;
import com.util.Constant;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.rmi.RemoteException;
import java.util.List;

/**
 * Created by Phillip on 2014/4/19.
 */
public class RMIBrokerInterfaceImpl implements RMIBrokerInterface {


    private BrokerRMIClient brokerRMIClient;
    private BrokerCORBAClient brokerCORBAClient;
    private UserDaoImpl userDao = new UserDaoImpl();
    private Logger logger;

    public RMIBrokerInterfaceImpl() {

        brokerRMIClient = new BrokerRMIClient();
        brokerCORBAClient = new BrokerCORBAClient();
        logger = Logger.getLogger(RMIBrokerInterfaceImpl.class);
        PropertyConfigurator.configure(Constant.BROKER_PRO_STRING);

    }

    @Override
    public List<Flight> queryFlights(int table, String deptString, String destString, String deptDateString, String returnDateString) throws RemoteException {
        if (table == 1) {
            logger.debug("request queryFlights on server 1");
            return brokerRMIClient.queryFlights(deptString, destString, deptDateString, returnDateString);
        } else if (table == 2) {
            logger.debug("request queryFlights on server 2");
            return brokerCORBAClient.queryFlights(deptString, destString, deptDateString, returnDateString);
        }
        return null;

    }

    @Override
    public List<String> queryCity(int table) throws RemoteException {
        if (table == 1) {
            logger.debug("request queryCity on server 1");
            return brokerRMIClient.queryCity();
        } else if (table == 2) {
            logger.debug("request queryCity on server 2");
            return brokerCORBAClient.queryCity();
        }
        return null;
    }

    @Override
    public String book(int table, String flightNoString1, String deptString, String classString1, String flightNoString2, String returnString, String classString2, String nameString, String creditString) throws RemoteException {
        if (table == 1) {
            logger.debug("request book on server 1");
            return brokerRMIClient.book(flightNoString1, deptString, classString1, flightNoString2, returnString, classString2, nameString, creditString);
        } else if (table == 2) {
            logger.debug("request book on server 2");
            return brokerCORBAClient.book(flightNoString1, deptString, classString1, flightNoString2, returnString, classString2, nameString, creditString);
        }
        return null;
    }

    @Override
    public String queryRates(int table, String flightString, String classString) throws RemoteException {
        if (table == 1) {
            logger.debug("request queryRates on server 1");
            return brokerRMIClient.queryRates(flightString, classString);
        } else if (table == 2) {
            logger.debug("request queryRates on server 2");
            return brokerCORBAClient.queryRates(flightString, classString);
        }
        return null;
    }

    @Override
    public String queryAirline(int table, String deptString, String dsctString) throws RemoteException {
        if (table == 1) {
            logger.debug("request queryAirline on server 1");
            return brokerRMIClient.queryAirline(deptString, dsctString);
        } else if (table == 2) {
            logger.debug("request queryAirline on server 2");
            return brokerCORBAClient.queryRates(deptString, dsctString);
        }
        return null;
    }

    @Override
    public boolean createUser(User user) throws RemoteException {
        logger.debug("request createUser");
        return userDao.createUser(user);
    }

    @Override
    public User getUser(String username) throws RemoteException {
        logger.debug("request a user by the username: " + username);
        return userDao.queryUser(username);
    }

}
